package com.qf.fmall2202.order.controller;

import com.qf.fmall2202.order.entity.Orders;
import com.qf.fmall2202.order.service.IOrdersService;
import com.qf.fmall2202.order.vo.AddOrderReturnVo;
import com.qf.fmall2202.utils.R;
import com.qf.fmall2202.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * <p>
 * 订单  前端控制器
 * </p>
 *
 * @author dong_
 * @since 2022-11-09
 */
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    IOrdersService ordersService;

    /**
     * 查询订单状态
     * @param orderId
     * @return
     */
    @GetMapping("/status/{orderId}")
    public R status(@PathVariable("orderId") String orderId) throws Exception {

        String status =  ordersService.status(orderId);

        return R.ok().put("data", status);

    }


    @PostMapping("/add")
    public R add(String cids,@RequestBody Orders order) throws Exception {

        AddOrderReturnVo vo  = ordersService.add(cids,order);
        return R.ok().put("data", vo);

    }

    /**
     * 模拟微信支付回调接口
     * @param oid
     * @return
     */
    @GetMapping("/callback")
    public R payCallBack(String oid) throws IOException {

        // 当接口被调用时就认为订单支付成功了
        WebSocketServer.sendMsg(oid,"1");

        return R.ok();

    }

}
